package controller;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.JComboBox;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.AppModel;
import view.AppView;

public class ListingController {

	// Application model and view
	private AppModel model;
	private AppView view;
	
	/**
	 * Constructor takes the view and the model
	 * @param model
	 * @param view
	 */
	public ListingController(AppModel model, AppView view) {
		this.model = model;
		this.view = view;
		assignHandlersToEvents();
	}

	/**
	 * Assigns event handlers to its the listing view
	 */
	private void assignHandlersToEvents() {
		
		// Move button click
		view.getListing().getMoveBtn().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionMoveToFolder();
			}
		});
		
		// Delete button click
		view.getListing().getDeleteBtn().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionDeleteEmail();
			}
		});
		
		// Right click on any row in the emails table
		view.getListing().getEmailsTable().addMouseListener(new MouseAdapter(){
			@Override
			public void mousePressed( MouseEvent e ){
				if ( SwingUtilities.isRightMouseButton(e)){
					actionShowRightClickMenu(e);
				}
				
			}
		});
		
		// Email selection detection
		view.getListing().getEmailsTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (e.getValueIsAdjusting() == false){
					actionEmailSelectionChanged();
				}
				
			}
		});
		
	}
	
	/**
	 * fired when email selection in the list has changed
	 */
	protected void actionEmailSelectionChanged() {
		System.out.println("Email list selection changed"); // temp
		
	}

	/**
	 * Show right click menu when a row is right clicked on the table
	 * @param me The mouse event
	 */
	protected void actionShowRightClickMenu(MouseEvent e) {
		
		// Selecting the currently right clicked row in the table
		JTable emailsTable = view.getListing().getEmailsTable();
		Point p = e.getPoint();
		int rowNumber = emailsTable.rowAtPoint( p );
		ListSelectionModel selectionModel = emailsTable.getSelectionModel();
		selectionModel.setSelectionInterval( rowNumber, rowNumber );

		// Creating the popup menu with options
		JPopupMenu popup = new JPopupMenu("Email options");
		JMenuItem moveItem = new JMenuItem(new AbstractAction("Move") {
		    public void actionPerformed(ActionEvent e) {
		        actionMoveToFolder();
		    }
		});
		JMenuItem deleteItem = new JMenuItem(new AbstractAction("Delete") {
		    public void actionPerformed(ActionEvent e) {
		        actionDeleteEmail();
		    }
		});
		
		popup.add(moveItem);
		popup.add(deleteItem);
		popup.show(e.getComponent(), e.getX(), e.getY());
	}

	/**
	 * Delete an email action
	 * @param e
	 */
	protected void actionDeleteEmail() {
		
		// Get what email items were currently selected and confirm of their deletion
		
		int option = JOptionPane.showConfirmDialog (null, "Are you sure you want to delete the selected emails?");
		if (option == JOptionPane.YES_OPTION ) {
			// Delete the emails
			System.out.println("Delete the emails");
		}
		
	}

	/**
	 * Move to folder popup
	 * @param e 
	 */
	private void actionMoveToFolder(){

		Object[] temp = {"Folder 1", "Folder 2"};
		JOptionPane popup = new JOptionPane();
		JComboBox foldersComboBox = new JComboBox(temp);
		JOptionPane.showMessageDialog(
		  null,
		  foldersComboBox,
		  "Move to folder",
		  JOptionPane.PLAIN_MESSAGE);
		System.out.println("Move to folder: " + foldersComboBox.getSelectedItem());
	}
	

}
